package com.yzt.minemodule.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.BlackListBean;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.BlackListAdapter;
import com.yzt.plugin_network.response.HttpResponse;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   : 黑名单列表
 */
public class BlackListActivity extends BaseActivity implements OnRefreshListener {

    private BlackListAdapter blackListAdapter;

    private RefreshLayout mRefreshLayout;

    public static void start(Activity activity) {
        activity.startActivity(new Intent(activity, BlackListActivity.class));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_black_list;
    }

    @Override
    public void installViews() {
        
        mTitleBar.setTitle(R.string.setting_black_list);

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);

        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        blackListAdapter = new BlackListAdapter();
        recycle.setAdapter(blackListAdapter);

        blackListAdapter.setOnItemClickListener((adapter, view, position) -> {
            BlackListBean entry = (BlackListBean) adapter.getItem(position);
            RouterUser.PersonalDetailActivity(entry.getUidRel());
        });

        blackListAdapter.addChildClickViewIds(R.id.remove_tv);
        blackListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.remove_tv) {
                    BlackListBean entry = (BlackListBean) adapter.getItem(position);
                    showMProgress();
                    // 移除黑名单
                    BizLayer.getInstance().getAccountModule().cancelBlack(entry.getUidRel(), 0, new AresSubscriber<HttpResponse>() {
                        @Override
                        protected void onSuccess(HttpResponse httpResponse) {
                            blackListAdapter.remove(position);
                            if (blackListAdapter.getData().size() == 0) {
                                blackListAdapter.setEmptyView(getEmptyView());
                            }
                        }

                        @Override
                        protected void onFinallyEnd() {
                            super.onFinallyEnd();
                            closeProgress();
                        }
                    });
                }
            }
        });

        blackListAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getHttpData();
        });

        mRefreshLayout.autoRefresh();
    }

    /**
     * 获取黑名单列表
     */
    public void getHttpData() {
        BizLayer.getInstance().getAccountModule().blackList(mCurrentPage, PAGE_SIZE, new AresSubscriber<List<BlackListBean>>() {
            @Override
            protected void onSuccess(List<BlackListBean> list) {
                if (mCurrentPage == 1) {
                    if (list.size() == 0) {
                        blackListAdapter.setEmptyView(getEmptyView());
                    } else {
                        blackListAdapter.removeEmptyView();
                    }
                    blackListAdapter.setList(list);
                } else {
                    blackListAdapter.addData(list);
                }
                blackListAdapter.getLoadMoreModule().loadMoreComplete();
                if (list.size() < PAGE_SIZE) {
                    blackListAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    blackListAdapter.setEmptyView(getErrorView(info));
                }
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getHttpData();
    }
}

